package com.bloveoacom.weichi.activity;

import android.app.Dialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.support.constraint.Group;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bloveoacom.base.AppContext;
import com.bloveoacom.base.BaseActivity;
import com.bloveoacom.base.UserInfoBean;
import com.bloveoacom.http.HttpMethods;
import com.bloveoacom.http.RxHelper;
import com.bloveoacom.observer.BaseSubscriber2;
import com.bloveoacom.util.Constant;
import com.bloveoacom.util.DialogUtil;
import com.bloveoacom.util.GlideUtil;
import com.bloveoacom.util.GsonUtil;
import com.bloveoacom.util.PicCameraLocalUtil;
import com.bloveoacom.util.ShareUtils;
import com.bloveoacom.util.StringUtils;
import com.bloveoacom.util.ToastUtil;
import com.bloveoacom.weichi.R;
import com.bloveoacom.weichi.bean.PostDetailBean;
import com.bloveoacom.weichi.bean.SimpleBean;
import com.bloveoacom.weight.LlkjToolBar;
import com.bloveoacom.weight.ProgressWebView;

import java.io.File;

import butterknife.BindView;
import butterknife.OnClick;

import static com.bloveoacom.util.PicCameraLocalUtil.recycle;

/**
 * author: yjl
 * e-mail: 1271901673@qq.com
 * time  : 2018/9/21
 * desc  :
 */
public class PostDetail2Activity extends BaseActivity {


    @BindView(R.id.tv_post_id)
    TextView tvPostId;
    @BindView(R.id.tv_post_title)
    TextView tvPostTitle;
    @BindView(R.id.layout_share)
    ConstraintLayout layoutShare;
    @BindView(R.id.title_bar)
    LlkjToolBar mToolbar;
    @BindView(R.id.group_user_info)
    Group groupUserInfo;
    @BindView(R.id.group_suggest)
    Group group_suggest;
    @BindView(R.id.iv_head)
    ImageView ivHead;
    @BindView(R.id.iv_attention)
    ImageView ivAttention;
    @BindView(R.id.tv_username)
    TextView tvUsername;
    @BindView(R.id.tv_post_time)
    TextView tvPostTime;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.wv_content)
    ProgressWebView wvContent;
    @BindView(R.id.tv_end)
    TextView tvEnd;
    @BindView(R.id.tv_support)
    TextView tvSupport;
    @BindView(R.id.tv_oppose)
    TextView tvOppose;
    @BindView(R.id.btn_share)
    Button btnShare;

    @Override
    public int getLayoutResId() {
        return R.layout.activity_post_detail2;
    }

    @Override
    protected View getViewSetTB() {
        return mToolbar;
    }

    private String coinId;

    private String isVote;
    Bitmap bitmap;
    @Override
    public void initView() {
        mToolbar.setToolBar("内容详情",
                true, "", R.drawable.ic_back, false, "", LlkjToolBar.isNull);
    }
    PostDetailBean postDetailBean;
    @Override
    public void initData() {
        BaseSubscriber2<PostDetailBean> baseSubscriber2 = HttpMethods.getInstance().getHttpService().getCoinNewsDetail(UserInfoBean.getInstance().getToken(), coinId).compose(RxHelper.getData()).subscribeWith(new BaseSubscriber2<PostDetailBean>() {
            @Override
            public void onNext(PostDetailBean o) {
                super.onNext(o);
                postDetailBean = o;
                String url = StringUtils.isEmpty(postDetailBean.getLinkUrl()) ? StringUtils.getFullUrl(postDetailBean.getUrl(), 2)
                        : postDetailBean.getLinkUrl();
                wvContent.loadUrl(url);
                tvUsername.setText(postDetailBean.getUserName());
                tvTitle.setText(postDetailBean.getTitle());
                tvTime.setText(postDetailBean.getCreateTime());
                GlideUtil.loadCircleImage(ivHead, StringUtils.getFullUrl(postDetailBean.headImg, 0));
                tvPostTitle.setText(postDetailBean.getTitle());
                ivAttention.setSelected(Integer.valueOf(postDetailBean.isFollow)==1);
                tvPostId.setText("文章ID:"+coinId);
                tvPostTime.setText(postDetailBean.createTime);

                if(postDetailBean.getUserId().equals(UserInfoBean.getInstance().getUid())){
                    groupUserInfo.setVisibility(View.GONE);
                    tvTime.setVisibility(View.VISIBLE);
                    tvTime.setText(postDetailBean.createTime);
                }else{
                    groupUserInfo.setVisibility(View.VISIBLE);
                    tvTime.setVisibility(View.GONE);
                    tvTime.setText(postDetailBean.createTime);
                }
                setSupportAndShare();

            }
        });
        addDispose(baseSubscriber2);
    }
    private void setSupportAndShare(){
        if(postDetailBean.getUserId().equals(UserInfoBean.getInstance().getUid())){
            btnShare.setVisibility(View.VISIBLE);
            group_suggest.setVisibility(View.GONE);
            mToolbar.setRightButtonOnClickLinster(null);
            mToolbar.setRightImg(android.R.color.transparent);
        }else{
            //支持过的
            if("1".equals(isVote)){
                group_suggest.setVisibility(View.GONE);
                btnShare.setVisibility(View.VISIBLE);
                mToolbar.setRightButtonOnClickLinster(null);
                mToolbar.setRightImg(android.R.color.transparent);
            }else{
                group_suggest.setVisibility(View.VISIBLE);
                btnShare.setVisibility(View.GONE);
                mToolbar.setRightButtonOnClickLinster(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        share();
                    }
                });
                mToolbar.setRightImg(R.drawable.ic_post_share);
            }
        }
    }

    @Override
    public void getBundleExtras(Bundle extras) {
        coinId = extras.getString(Constant.DATA0);
        isVote = extras.getString(Constant.DATA1);
    }

    @Override
    public void initListener() {

    }

    File sharefile;
    @OnClick({R.id.iv_attention, R.id.tv_support, R.id.tv_oppose, R.id.btn_share})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_attention:
                BaseSubscriber2<Object> baseSubscriber2 = HttpMethods.getInstance().getHttpService().userFocus(UserInfoBean.getInstance().getToken(), postDetailBean.getUserId(), (Integer.valueOf(postDetailBean.isFollow) == 1 ? 1 : 0)).compose(RxHelper.getData()).subscribeWith(new BaseSubscriber2<Object>(this) {
                    @Override
                    public void onNext(Object o) {
                        //取消关注
                        if(Integer.valueOf(postDetailBean.isFollow) == 1){
                            postDetailBean.isFollow = "0";
                        }else{
                            //关注
                            postDetailBean.isFollow = "1";

                        }
                        ivAttention.setSelected(Integer.valueOf(postDetailBean.isFollow)==1);
                        ToastUtil.showCustomShort(mContext,getString(ivAttention.isSelected()?R.string.ok_attention:R.string.no_attention));
                    }
                });
                break;
            case R.id.tv_support:
                isVoted = 0;
                so();
                break;
            case R.id.tv_oppose:
                isVoted = 1;
                so();
                break;
            case R.id.btn_share:
                share();
                break;
        }
    }

    int isVoted = 0;
    private void so(){
        BaseSubscriber2<Object> baseSubscriber2 = HttpMethods.getInstance().getHttpService().goReviewCoinNew(UserInfoBean.getInstance().getToken(), coinId, String.valueOf(isVoted)).compose(RxHelper.getData()).subscribeWith(new BaseSubscriber2<Object>(this) {
            @Override
            public void onNext(Object o) {
                super.onNext(o);
                SimpleBean simpleBean = GsonUtil.GsonToBean(GsonUtil.GsonString(o), SimpleBean.class);
                if(isVoted==1&&simpleBean.isCallback==1){
                    BaseSubscriber2<Object> objectBaseSubscriber2 = HttpMethods.getInstance().getHttpService().notThroughCoinNew(coinId, UserInfoBean.getInstance().getToken()).compose(RxHelper.getData()).subscribeWith(new BaseSubscriber2<>());
                    addDispose(objectBaseSubscriber2);
                }
                isVote = "1";
                setSupportAndShare();
            }
        });
        addDispose(baseSubscriber2);
    }

    Dialog dialog;
    private void share() {
        if(bitmap==null||sharefile==null){
            bitmap = PicCameraLocalUtil.getBitmap(layoutShare);
            sharefile = PicCameraLocalUtil.saveBitmap(mContext, bitmap, "dsh_post2");
        }
        AppContext.mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                dialog = DialogUtil.getPicBottomDialog2(mContext, new DialogUtil.PhotoDoWhat() {
                    @Override
                    public void doone() {
                        ShareUtils.shareFriends(sharefile.getPath(), 0);
                        DialogUtil.dissDialog(dialog);
                        ToastUtil.makeShortText(mContext, "分享操作正在后台进行");
                    }

                    @Override
                    public void dotwo() {
                        ShareUtils.shareFriends(sharefile.getPath(), 1);
                        DialogUtil.dissDialog(dialog);
                        ToastUtil.makeShortText(mContext, "分享操作正在后台进行");
                    }

                    @Override
                    public void dothree() {

                    }
                }, sharefile);
                DialogUtil.showDialog(dialog);
            }
        }, 1000);
    }
    @Override
    public void onDestroy() {
        recycle(bitmap);
        super.onDestroy();
    }
}
